"use strict";
/* Controllers */
app.controller("olapConfigListController", ['$scope', '$compile' , '$location', '$rootScope', '$routeParams', 'olapConfigListService', function ($scope, $compile, $location, $rootScope, $routeParams, olapConfigListService) {

    var vm = $scope.vm;

    vm.p = {};
    vm.result = {};

    var errorFn = function (result) {
        alert(result);
    };

    $scope.listOlapConf = function () {
        var vm = $scope.vm;
        var params = {};
        var procFn = function (response) {
            var data = response.data;
            if (data == null) {
                return;
            }
            vm.result.data = data;
            $('#list_table').bootstrapTable('load', data);
        };
        olapConfigListService.listOlapConf(params,procFn, errorFn);
    };




    $scope.confirmDel=function(id,index){
        vm.toDelId=id
        vm.toDelIndex=index
        $('#myModal').modal('show')
    }

    $scope.ok=function(){
        var vm = $scope.vm;
        console.log("toDelId"+vm.toDelId)
        console.log("toDelIndex"+vm.toDelIndex)
        var params = {};
        params.id=vm.toDelId
        params.deleted=2
        var procFn = function (response) {
            if (response.success && response.data) {
               $('#list_table').bootstrapTable('remove', {
                   field:'id',
                   values:[vm.toDelId]
               });
               alert("删除成功！")
            } else {
               alert(response.msg);
            }
        };
        olapConfigListService.delOlapConf(params, procFn, errorFn);
        $('#myModal').modal('hide')
    }

    $scope.initializeController = function () {
        var $resultTable = $('#list_table');
        $resultTable.bootstrapTable({
             pageList: [30, 50, 100, 300, 500, 1000], pageSize: 30
        });
        $resultTable.bootstrapTable('hideColumn', 'sql');
        $resultTable.bootstrapTable('hideColumn', 'indicators');
        $resultTable.bootstrapTable('hideColumn', 'createTime');
        $scope.listOlapConf();
    };

    $scope.initializeController();
}]);

function olapConfigOpt(value, row, index) {
        var optCont='<a type="button" class="btn btn-xs btn-danger" onclick=angular.element(this).scope().confirmDel('+ row.id +','+index+') >删除</a>\n'
        var optCont2='<a type="button" class="btn btn-xs btn-primary" href="#/sre/config/olap_config/edit/'+row.id+'" target="_blank">修改</a>'
        return [
            optCont,
            optCont2
        ].join('');
}




/* Services */
app.service('olapConfigListService', ['ajaxService', function (ajaxService) {

//    this.listCostReport = function (params, successFunction, errorFunction) {
//        ajaxService.AjaxPost(params, "/star/api/basic-service/v1/cost_report/listCostReport.do", successFunction, errorFunction);
//    };
    this.listOlapConf = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "/star/api/rt-olap-service/api/alarm/olapSqlConf/get", successFunction, errorFunction);
    };

    this.delOlapConf = function (params, successFunction, errorFunction) {
        ajaxService.AjaxPost(params, "/star/api/rt-olap-service/api/alarm/olapSqlConf/update", successFunction, errorFunction);
    };

}]);


function tsFmt(value, row, index) {
    if (value == null) {
        return "-";
    }
    var n = parseInt(value);
    if(value.length==10) {
        n *= 1000;
    }
    return formatDate(n);
}

